package com.yjc.hire.work.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.JsonElement;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.google.common.reflect.TypeToken;
import com.yjc.hire.enums.JobStatus;
import com.yjc.hire.feign.UserMicroServiceFeign;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.utils.LocalDateUtils;
import com.yjc.hire.work.bo.EditJboBO;
import com.yjc.hire.work.bo.SearchBO;
import com.yjc.hire.work.bo.SearchJobSBO;
import com.yjc.hire.work.mapper.JobMapper;
import com.yjc.hire.work.service.JobService;
import com.yjc.hire.work.vo.SearchJobVO;
import com.yjc.hire.work.vo.UsersVO;
import com.yjc.pojo.Job;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/8/26
 * </p>
 */
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private UserMicroServiceFeign userMicroServiceFeign;

    @Override
    public void modifyJobDetail(EditJboBO editJboBO) {
        Job job = new Job();
        BeanUtils.copyProperties(editJboBO, job);
        job.setUpdatedTime(LocalDateTime.now());
        if (StringUtils.isBlank(editJboBO.getId())) {

            job.setStatus(JobStatus.OPEN.type);
            jobMapper.insert(job);
        }else {
            //修改
            jobMapper.update(
                    job,new LambdaQueryWrapper<Job>()
                            .eq(Job::getId, editJboBO.getId())
                            .eq(Job::getHrId, editJboBO.getHrId())
                            .eq(Job::getCompanyId, editJboBO.getCompanyId())
            );
        }
    }

    @Override
    public PageGridResult<List<SearchJobVO>> searchJobs(Integer page, Integer limit, SearchJobSBO searchJobSBO) {
        String jobName = searchJobSBO.getJobName();
        String jobType = searchJobSBO.getJobType();
        String city = searchJobSBO.getCity();
        String beginSalary = searchJobSBO.getBeginSalary();
        String endSalary = searchJobSBO.getEndSalary();
        PageHelper.startPage(page, limit);
        LambdaQueryWrapper<Job> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(jobName)) {
            queryWrapper.like(Job::getJobName, jobName);
        }
        queryWrapper.eq(Job::getStatus, JobStatus.OPEN.type);
        if (StringUtils.isNotBlank(jobType)) {
            queryWrapper.like(Job::getJobType, jobType);
        }
        if (StringUtils.isNotBlank(city)) {
            queryWrapper.like(Job::getCity, city);
        }
        if (StringUtils.isNotBlank(beginSalary)) {
            queryWrapper.ge(Job::getBeginSalary, beginSalary);
        }
        if (StringUtils.isNotBlank(endSalary)) {
            queryWrapper.le(Job::getEndSalary, endSalary);
        }
        List<Job> joblist = jobMapper.selectList(queryWrapper);
        //额外操作
        if (joblist == null || joblist.isEmpty()) {
            return PageGridResult.of(Collections.emptyList(), page);
        }
        List<String> companyIdList = new ArrayList<>();
        List<String> hrIdList = new ArrayList<>();
        List<SearchJobVO> searchJobVOList = new ArrayList<>();
        for (Job job : joblist) {
            hrIdList.add(job.getHrId());
            companyIdList.add(job.getCompanyId());
            SearchJobVO jobVO = new SearchJobVO();
            BeanUtils.copyProperties(job, jobVO);
            searchJobVOList.add(jobVO);
        }
        com.yjc.bo.SearchBO searchBO = new com.yjc.bo.SearchBO();
        searchBO.setUserIds(hrIdList);
        GraceJSONResult list = userMicroServiceFeign.getList(searchBO);
        String  data = (String) list.getData();
        List<UsersVO> json = new Gson().fromJson(data,new TypeToken<List<UsersVO>>(){}.getType());
        for (SearchJobVO jobVO : searchJobVOList) {
            for (UsersVO usersVO : json) {
                if (usersVO.getId().equals(jobVO.getHrId())) {
                    jobVO.setHrId(usersVO.getId());
                }
            }
        }
        return PageGridResult.of(searchJobVOList, page);
    }
}
